/*
 *   The contents of this file are subject to the Mozilla Public License
 *   Version 1.1 (the "License"); you may not use this file except in
 *   compliance with the License. You may obtain a copy of the License at
 *   http://www.mozilla.org/MPL/
 *
 *   Software distributed under the License is distributed on an "AS IS"
 *   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 *   License for the specific language governing rights and limitations
 *   under the License.
 *
 *   The Original Code is OkkamNGClient, in the package org.okkam.client.*.
 *
 *   The Initial Developer of the Original Code is 
 *		Heiko Stoermer (http://disi.unitn.it/~stoermer).
 *
 *   Portions created by the Initial Developer are Copyright (C) 2007
 *   the Initial Developer. All Rights Reserved.
 *
 *   Contributor(s): Daniel Giacomuzzi (etadaniel@gmail.com).
 *
*/
package org.okkam.client.wsclient;

import java.io.StringWriter;
import java.rmi.RemoteException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.okkam.core.api.data.entityprofile.EntityProfile;
import org.okkam.webservice.EntityProfilePublicationServiceProxy;

/**
 * Webservice client that publishes an entity profile.
 * 
 * <h2>Example</h2>
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = -->
<!-- =   Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard  markus@jave.de   = -->
<!-- =     Further information: http://www.java2html.de     = -->
<div align="left" class="java">
<table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff">
   <tr>
  <!-- start source code -->
   <td nowrap="nowrap" valign="top" align="left">
    <code>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">EntityProfilePublicationClient&nbsp;client&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">EntityProfilePublicationClient</font><font color="#000000">()</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">EntityProfile&nbsp;profile&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">EntityProfile</font><font color="#000000">()</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">LabelsType&nbsp;labels&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">LabelsType</font><font color="#000000">()</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">LabelType&nbsp;label1&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">LabelType</font><font color="#000000">()</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">label1.setPrefix</font><font color="#000000">(</font><font color="#2a00ff">&#34;foaf:firstname&#34;</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">label1.setValue</font><font color="#000000">(</font><font color="#2a00ff">&#34;Mario&#34;</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">labels.getLabel</font><font color="#000000">()</font><font color="#000000">.add</font><font color="#000000">(</font><font color="#000000">label1</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">LabelType&nbsp;label2&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">LabelType</font><font color="#000000">()</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">label2.setPrefix</font><font color="#000000">(</font><font color="#2a00ff">&#34;foaf:lastname&#34;</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">label2.setValue</font><font color="#000000">(</font><font color="#2a00ff">&#34;Zeni&#34;</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">labels.getLabel</font><font color="#000000">()</font><font color="#000000">.add</font><font color="#000000">(</font><font color="#000000">label2</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">profile.setLabels</font><font color="#000000">(</font><font color="#000000">labels</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">ReferencesType&nbsp;references&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">ReferencesType</font><font color="#000000">()</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">ReferenceType&nbsp;r1&nbsp;=&nbsp;</font><font color="#7f0055"><b>new&nbsp;</b></font><font color="#000000">ReferenceType</font><font color="#000000">()</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">r1.setType</font><font color="#000000">(</font><font color="#2a00ff">&#34;2&#34;</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">r1.setValue</font><font color="#000000">(</font><font color="#2a00ff">&#34;www.okkam.org&#34;</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">references.getReference</font><font color="#000000">()</font><font color="#000000">.add</font><font color="#000000">(</font><font color="#000000">r1</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">profile.setReferences</font><font color="#000000">(</font><font color="#000000">references</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">profile.setWordnetIdentifier</font><font color="#000000">(</font><font color="#2a00ff">&#34;100007320&#34;</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><b>try&nbsp;</b></font><font color="#000000">{</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">String&nbsp;uri&nbsp;=&nbsp;client.publish</font><font color="#000000">(</font><font color="#000000">profile</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#000000">uri</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}&nbsp;</font><font color="#7f0055"><b>catch&nbsp;</b></font><font color="#000000">(</font><font color="#000000">RemoteException&nbsp;exc</font><font color="#000000">)&nbsp;{</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#000000">exc.getMessage</font><font color="#000000">())</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font></code>
    
   </td>
  <!-- end source code -->
   </tr>
</table>
</div>

 * 
 * @author etadaniel
 *
 */
public class EntityProfilePublicationClient {

	private final static Log log = LogFactory
			.getLog(EntityProfilePublicationClient.class);
	EntityProfilePublicationServiceProxy proxy;

	public EntityProfilePublicationClient() {
		this.proxy = new EntityProfilePublicationServiceProxy();
	}

	/**
	 * Method to publishes an entity profile.
	 * @param ep A populated instance of {@link org.okkam.core.api.data.entityprofile.EntityProfile}
	 * @return A string containint the resulting OKKAM ID, or null if publication failed.
	 * @throws IllegalArgumentException
	 * @throws RemoteException
	 */
	public String publish(EntityProfile ep) throws IllegalArgumentException,
			RemoteException {
		if (ep != null) {
			StringWriter profileXml = new StringWriter();
			log.debug("trying to marshal profile.");
			try {
				JAXBContext jc = JAXBContext.newInstance("org.okkam.core.api.data.entityprofile",getClass().getClassLoader());
				Marshaller m = jc.createMarshaller();
				m.marshal(ep, profileXml);
				String result = proxy.publish(profileXml.toString());
				return result;
			} catch (JAXBException e) {
				log
						.error("marshalling failed: "
								+ e.getStackTrace().toString());
				throw new IllegalArgumentException("Marshalling failed, invalid XML representation:" + e.getMessage());
			}
			
			
		} else {
			log.error("Impossible to  process a null EntityProfile");
			throw new IllegalArgumentException(
					"Impossible to  process a null EntityProfile");
		}
	}
}
